
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System;

public class GameActionViewer : EditorWindow
{
	UnityEngine.Object[] actionPopupArray = null;

	string[] actionPopupStringArray = null;
	int actionPopupSelect = 0;

	GameObject gameObject = null;

	bool showRole = false;

	string[] actionTypePopupStringArray = { SAActionType.ATTACK.ToString() , 
		SAActionType.HURT.ToString() , 
		SAActionType.FALL.ToString() ,
		SAActionType.STAND.ToString() ,
		SAActionType.MOVE.ToString() , 
		SAActionType.SIT.ToString() , 
		SAActionType.HAND.ToString() , 
		SAActionType.HAPPY.ToString() , 
		SAActionType.ANGER.ToString() ,
		SAActionType.CRY.ToString() , 
		SAActionType.DEFENCE.ToString() ,
		SAActionType.HEAD.ToString() , 
		SAActionType.THROW.ToString() };
	int actionTypePopupSelect = 0;

	string[] directionPopupStringArray = { SAMapDirectionType.SOUTHWEST.ToString() , 
		SAMapDirectionType.WEST.ToString() ,
		SAMapDirectionType.NORTHWEST.ToString() ,
		SAMapDirectionType.NORTH.ToString() ,
		SAMapDirectionType.NORTHEAST.ToString() ,
		SAMapDirectionType.EAST.ToString() ,
		SAMapDirectionType.SOUTHEAST.ToString() ,
		SAMapDirectionType.SOUTH.ToString() ,
	};
	int directionPopupSelect = 0;




	string[] rolePopupStringArray = { 
		SARoleType.DOUDING.ToString() , 
		SARoleType.SAIYA.ToString() ,
		SARoleType.HOUZI.ToString() ,
		SARoleType.XIANDAN.ToString() ,
		SARoleType.DASHU.ToString() ,
		SARoleType.QIUZHANG.ToString() ,

		SARoleType.DOUDINGMEI.ToString() ,
		SARoleType.NAIYOU.ToString() ,
		SARoleType.MEIZI.ToString() ,
		SARoleType.FAJIA.ToString() ,
		SARoleType.WUDAOJIA.ToString() ,
		SARoleType.DAJIE.ToString() ,

	};
	int rolePopupSelect = 0;
	
	string[] roleColorPopupStringArray = { 
		SARoleColorType.COLOR0.ToString() ,
		SARoleColorType.COLOR1.ToString() ,
		SARoleColorType.COLOR2.ToString() ,
		SARoleColorType.COLOR3.ToString() ,
		SARoleColorType.COLOR4.ToString() ,
		SARoleColorType.COLOR5.ToString() ,

	};
	int roleColorPopupSelect = 0;

	string[] roleRidingPopupStringArray = { 

		SARoleRidingType.NORMAL.ToString() ,

		SARoleRidingType.BRONTOSAURUS.ToString() ,
		SARoleRidingType.TIGER.ToString() ,

		SARoleRidingType.BLUE_TYRANNOSAURUS.ToString() ,
		SARoleRidingType.RED_TYRANNOSAURUS.ToString() ,
		SARoleRidingType.ROBOT_TYRANNOSAURUS.ToString() ,
		SARoleRidingType.GRAY_ELEPHANT.ToString() ,
		SARoleRidingType.GOLD_PTEROSAUR.ToString() ,
		SARoleRidingType.RED_DOG.ToString() ,
		SARoleRidingType.BLUE_DRAGON.ToString() ,
		SARoleRidingType.GREEN_PANGOLIN.ToString() ,
		SARoleRidingType.RED_BEETLE.ToString() ,
		SARoleRidingType.RED_ORANGUTAN.ToString() ,

	};
	int roleRidingPopupSelect = 0;

	string[] weaponPopupStringArray = { 
		
		SAItemType.FIST.ToString() , 
		SAItemType.AXE.ToString() , 
		SAItemType.CLUB.ToString() , 
		SAItemType.SPEAR.ToString() , 
		SAItemType.BOW.ToString() , 
	};
	int weaponPopupSelect = 0;


	void OnGUI()
	{
		GUI.color = Color.green;
		EditorGUILayout.LabelField( "Action Finder:" , EditorStyles.boldLabel );
		GUI.color = Color.white;

		EditorGUILayout.BeginHorizontal();

		if ( GUILayout.Button("Reload" ) )
		{
			loadData();

			if ( actionPopupArray == null || actionPopupArray.Length == 0 )
			{
				ShowNotification( new GUIContent("No Action selected for searching") );
				return;
			}
		}

		EditorGUILayout.EndHorizontal();

		if ( actionPopupArray == null || actionPopupArray.Length == 0 )
		{
			return;
		}

		EditorGUILayout.LabelField( "Action Length:" + actionPopupArray.Length );

		showActionFinder();

		GUI.color = Color.green;
		EditorGUILayout.LabelField( "Action Viewer:" , EditorStyles.boldLabel );
		GUI.color = Color.white;

		if ( !GameActionViewerTestUIHandler.instance ) 
		{
			GUI.color = Color.red;
			GUILayout.Label( "Please Start TestViewer Scene." , EditorStyles.boldLabel );
			GUI.color = Color.white;

			gameObject = null;

			return;
		}

		
		
		if ( gameObject != null ) 
		{
			SACreatureAction creatureAction = gameObject.GetComponent<SACreatureAction>();
			
			if ( actionPopupSelect != creatureAction.actionID )
			{
				string selstr = actionPopupStringArray[ actionPopupSelect ];
				int sel = Convert.ToInt32( selstr );
				
				creatureAction.setActionID( sel );
				creatureAction.playAction( actionTypePopupSelect , directionPopupSelect );
				creatureAction.setDirection( directionPopupSelect );
			}
			
			if ( actionTypePopupSelect != creatureAction.actionType )
			{
				creatureAction.playAction( actionTypePopupSelect , directionPopupSelect );
			}
			
			if ( directionPopupSelect != creatureAction.direction )
			{
				creatureAction.setDirection( directionPopupSelect );
			}

		}


		if ( actionPopupArray != null && actionPopupArray.Length != 0 )
		{
			showAction();
		}

		GUI.color = Color.red;
		GUILayout.Label( "Role:" , EditorStyles.boldLabel );
		GUI.color = Color.white;

		
		showRole = EditorGUILayout.Toggle( "Show Role" , showRole );

		showRoleAction();


		if ( showRole && gameObject != null ) 
		{
			SACreatureAction creatureAction = gameObject.GetComponent<SACreatureAction>();

			int id1 = SARoleConfig.instance.getActionID( rolePopupSelect , roleColorPopupSelect , roleRidingPopupSelect , weaponPopupSelect );
			
			if ( id1 != creatureAction.actionID  )
			{
				creatureAction.setActionID( id1 );
				creatureAction.playAction( actionTypePopupSelect , directionPopupSelect );
				creatureAction.setDirection( directionPopupSelect );
				
			}
		}
			
	}
	

	void showActionFinder()
	{
		EditorGUILayout.BeginHorizontal();
		
		actionPopupSelect = EditorGUILayout.Popup( actionPopupSelect , actionPopupStringArray );

		EditorGUILayout.EndHorizontal();


		EditorGUILayout.BeginHorizontal();
		
		actionTypePopupSelect = EditorGUILayout.Popup( actionTypePopupSelect , actionTypePopupStringArray );
		directionPopupSelect = EditorGUILayout.Popup( directionPopupSelect , directionPopupStringArray );

		EditorGUILayout.EndHorizontal();
	}


	void showRoleAction()
	{
		EditorGUILayout.BeginHorizontal();
		
		rolePopupSelect = EditorGUILayout.Popup( rolePopupSelect , rolePopupStringArray );
		roleColorPopupSelect = EditorGUILayout.Popup( roleColorPopupSelect , roleColorPopupStringArray );
		roleRidingPopupSelect = EditorGUILayout.Popup( roleRidingPopupSelect , roleRidingPopupStringArray );
		weaponPopupSelect = EditorGUILayout.Popup( weaponPopupSelect , weaponPopupStringArray );

		EditorGUILayout.EndHorizontal();
	}

	void showAction()
	{

		EditorGUILayout.BeginHorizontal();

		if ( GUILayout.Button("Start!" ) )
		{
			gameObject = GameObject.Find( "Sprite1" );

			string selstr = actionPopupStringArray[ actionPopupSelect ];
			int sel = Convert.ToInt32( selstr );

			SACreatureAction creatureAction = gameObject.GetComponent<SACreatureAction>();

			creatureAction.setActionID( sel );
			creatureAction.playAction( actionTypePopupSelect , directionPopupSelect );
		}

		if ( GUILayout.Button("Clear" ) )
		{
			if ( gameObject != null )
			{
				SACreatureAction creatureAction = gameObject.GetComponent<SACreatureAction>();
				creatureAction.clearAction();
			}

			gameObject = null;
		}

		EditorGUILayout.EndHorizontal();


	}
	
	
	public void loadData()
	{
		if ( actionPopupArray != null )
		{
			actionPopupArray = null;
		}

		actionPopupArray = (UnityEngine.Object[])GameEditorDefine.GetAssets(  ".actz" );

		if ( actionPopupArray == null )
		{
			return;
		}

		actionPopupStringArray = new string[ actionPopupArray.Length ];

		for ( int i = 0 ; i < actionPopupArray.Length ; i++ )
		{
			actionPopupStringArray[ i ] = actionPopupArray[ i ].name;
		}

	}




}
